package com.xd.common.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.HashSet;

/**
 * @Author: 林顺
 * @Date: 2021/11/20 15:48
 */
@Slf4j
@WebFilter(filterName = "ipFilter",urlPatterns = "/fans/*")
public class IpFilter implements Filter {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private HashSet<String> ip_list = new HashSet<>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ip_list.add("157.0.47.1");
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String add_ip = servletRequest.getRemoteAddr();
        log.info(add_ip);
        if(ip_list.contains(add_ip)){
            log.info("拦截" + add_ip);
        }else{
            log.info("通过" + add_ip);
        }
    }

    @Override
    public void destroy() {
        log.info("要销毁了");
        Filter.super.destroy();
    }

}
